package net.begincode.admin.controller;

import net.begincode.admin.bean.BlogIdParam;
import net.begincode.biz.BlogBiz;
import net.begincode.blog.bean.param.BlogQueryParam;
import net.begincode.blog.bean.vo.CheckBlog;
import net.begincode.blog.entity.Blog;
import net.begincode.blog.enums.BlogCheckFlagEnum;
import net.begincode.blog.enums.BlogFlagEnum;
import net.begincode.common.ApiResponse;
import net.begincode.design.BizTemplate;
import net.begincode.page.PageRequest;
import net.begincode.page.PageResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.awt.*;
import java.util.List;

/**
 * 博文管理
 */
@Controller
@RequestMapping("admin/blog")
public class BlogController {
    @Resource
    private BlogBiz blogBiz;

    /**
     * 查看待审核博文
     */
    @RequestMapping("queryWaitCheckBlog")
    @ResponseBody
    public ApiResponse<List<CheckBlog>> queryWaitCheckBlog(BlogQueryParam queryParam) {
        return new BizTemplate<ApiResponse<List<CheckBlog>>>("BlogController.queryWaitCheckBlog") {
            @Override
            public void checkParam() {
                super.checkParam();
            }
            @Override
            public ApiResponse<List<CheckBlog>> process() {
                PageResponse<CheckBlog> pager = blogBiz.queryCheckBlog(queryParam);
                return ApiResponse.success(pager.getData(),pager.getTotalSize());
            }
        }.excute();

    }

    /**
     * 审核通过
     */
    @RequestMapping("checkBlogPass")
    @ResponseBody
    public ApiResponse<Boolean> checkBlogPass(BlogIdParam blogIdParam) {
        blogIdParam.checkParam();
        Blog checkPassBlog = new Blog();
        checkPassBlog.setId(blogIdParam.getBlogId());
        checkPassBlog.setCheckFlag(BlogCheckFlagEnum.PASS.getFlagCode());
        int returnValue = blogBiz.updateBlogById(checkPassBlog);
        if (returnValue > 0) {
            return ApiResponse.success(true);
        }
        return ApiResponse.fail(false);
    }

    /**
     * 审核不通过
     */
    @RequestMapping("checkBlogFail")
    @ResponseBody
    public ApiResponse<Boolean> checkBlogFail(BlogIdParam blogIdParam) {
        blogIdParam.checkParam();
        Blog checkPassBlog = new Blog();
        checkPassBlog.setId(blogIdParam.getBlogId());
        checkPassBlog.setCheckFlag(BlogCheckFlagEnum.FAIL.getFlagCode());
        int returnValue = blogBiz.updateBlogById(checkPassBlog);
        if (returnValue > 0) {
            return ApiResponse.success(true);
        }
        return ApiResponse.fail(false);
    }


}
